#include <a_samp>
#define MPH 0 // If you do not want to use KMP write 0.61, if yes write 0

public OnFilterScriptInit()	return print("\n--------------------------------------"), print("eSpeedCheck by Epsilon"), print("-------------loaded-------------------\n"), 1;
public OnFilterScriptExit() return print("\n--------------------------------------"), print("eSpeedCheck by Epsilon"), print("-----------unloaded-------------------\n"), 1;
new bool: active[MAX_PLAYERS],bool: fi[MAX_PLAYERS][MAX_PLAYERS];
public OnPlayerConnect(playerid)
{
	for(new i = 0; i < MAX_PLAYERS; i++) if(IsPlayerConnected(i)) fi[playerid][i] = false;
	active[playerid] = false;
	return 1;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/speedc-on", cmdtext, true) == 0) if(!s_IsCop(playerid)) return SendClientMessage(playerid,0xAA3333AA,"You have to be a cop!"); else return active[playerid] = true, SendClientMessage(playerid,0xAA3333AA,"Speedcam activated!"),CheckS(playerid);	else if (strcmp("/speedc-off", cmdtext, true) == 0) if(!s_IsCop(playerid)) return SendClientMessage(playerid,0xAA3333AA,"You have to be a cop!"); else return active[playerid] = false,SendClientMessage(playerid,0xAA3333AA,"Speedcam deactivated!");
	return 0;
}
forward CheckS(playerid);
forward Unfi(i,id);
public Unfi(i,id) return fi[i][id] = false, 1;
public CheckS(playerid)
{
	new id = s_GetClosestPlayer(playerid,10.0),estr[128];
	if(!IsPlayerConnected(id) || s_IsCop(id) || !active[playerid] || fi[playerid][id] || GetPlayerState(id) != 2 ||!GetSpeed(id)) return SetTimerEx("CheckS",500,0,"i",playerid),1;
	format(estr,128,"%s drives at %d speed.",GET_PLAYER_NAME(id),GetSpeed(id));
	fi[playerid][id] = true;
	SendClientMessage(playerid,0xAA3333AA,estr);
	SetTimerEx("Unfi",7*1000,0,"ii",playerid,id);
	SetTimerEx("CheckS",500,0,"i",playerid);
	return 1;
}
s_IsCop(playerid) if(GetPlayerSkin(playerid) == 280 || GetPlayerSkin(playerid) == 281|| GetPlayerSkin(playerid) == 282 || GetPlayerSkin(playerid) == 283|| GetPlayerSkin(playerid) ==284|| GetPlayerSkin(playerid) ==288 ) return 1; else return 0;
Float:GetPos(playerid,posid) { new Float:id[3]; if(IsPlayerInAnyVehicle(playerid)) GetVehiclePos(GetPlayerVehicleID(playerid),id[0],id[1],id[2]); else GetPlayerPos(playerid,id[0],id[1],id[2]); return id[posid-1]; }
GetSpeed(id) { new Float:s[4]; GetVehicleVelocity(GetPlayerVehicleID(id),s[0],s[1],s[2]); return floatround((1.61-MPH)*floatsqroot(floatabs(floatpower(s[0]+s[1]+s[2],2)))*100); }
GET_PLAYER_NAME( id ) { new x[  MAX_PLAYER_NAME  ]; if( IsPlayerConnected( id ) ) GetPlayerName( id , x , sizeof(x) ); return x; }
s_GetClosestPlayer(except,Float: radius = 99999.9999)
{
	new Float:dis[2],e[2];
	e[0] = INVALID_PLAYER_ID;
	dis[0] = radius;
	for (e[1]=0;e[1]<MAX_PLAYERS;e[1]++) if(IsPlayerConnected(e[1]) && e[1] != except && !fi[except][e[1]])
	{
		dis[1] = floatsqroot(floatpower(floatabs(floatsub(GetPos(e[1],1),GetPos(except,1))),2)+floatpower(floatabs(floatsub(GetPos(e[1],2),GetPos(except,2))),2)+floatpower(floatabs(floatsub(GetPos(e[1],3),GetPos(except,3))),2));
		if(dis[1]< dis[0])
		{
			dis[0] = dis[1];
			e[0] = e[1];
		}
	}
	return e[0];
}
